package com.weiyan.controller;

import com.weiyan.domian.Product;
import com.weiyan.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @author misterWei
 * @create 2020年02月20号:20点19分
 * @mailbox mynameisweiyan@gmail.com
 */
@RestController
@CrossOrigin
@RequestMapping("product")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;


    @RequestMapping(value = "api1/demo1",method = RequestMethod.GET)
    public String demo1(){
        return "demo";
    }

    @RequestMapping(value = "api1/demo2",method = RequestMethod.GET)
    public String demo2(){
        return "demo";
    }
    @RequestMapping(value = "api2/demo1",method = RequestMethod.GET)
    public String demo3(){
        return "demo";
    }
    @RequestMapping(value = "api2/demo2",method = RequestMethod.GET)
    public String demo4(){
        return "demo";
    }



    //查询产品信息
    @RequestMapping(value = "findByPid/{pid}",method = RequestMethod.GET)
    public Product findByPid(@PathVariable Integer pid, HttpServletRequest request){
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String nextElement = headerNames.nextElement();
            System.out.println("名称为: "+ nextElement +"---"+"值为: "+request.getHeader(nextElement));
        }
        log.info("获取标识为{}的产品信息00",pid);
        if (pid.equals(1)){
            throw new RuntimeException("调用错误~!!");
        }
        return productService.findByPid(pid);

    }

    /**
     * 下订单 减库存操作
     */
    @RequestMapping(value = "decrement/{pid}",method = RequestMethod.GET)
    public void decrement(@PathVariable Integer pid){
        productService.decrement(pid);
    }

}
